package Backend;

import java.io.IOException;
import org.snmp4j.agent.DuplicateRegistrationException;
import org.snmp4j.smi.OID;

public class PrinterAgent extends SnmpAgent {

    public static final OID vendor = new OID("1.3.6.1.2.1.43.8.2.1.0"); // static
    public static final OID model = new OID("1.3.6.1.2.1.43.8.2.2.0"); // static
    public static final OID serial = new OID("1.3.6.1.2.1.43.8.2.3.0"); // static
    public static final OID name = new OID("1.3.6.1.2.1.43.8.2.4.0"); // changable
    public static final OID max_capacity = new OID("1.3.6.1.2.1.43.8.2.5.0"); // static
    public static final OID capacity_level = new OID("1.3.6.1.2.1.43.8.2.6.0"); // static, but needs to be polled
    public static final OID localisation = new OID("1.3.6.1.2.1.43.8.2.7.0"); // changable
    public static final OID supports_colour = new OID("1.3.6.1.2.1.43.8.2.8.0"); // changable
    public static final OID colour_printing = new OID("1.3.6.1.2.1.43.8.2.9.0"); // changable
    public static final OID status = new OID("1.3.6.1.2.1.43.8.2.10.0");  // static, but needs to be polled
    public static final OID jobs = new OID("1.3.6.1.2.1.43.8.2.11.0");  // static, but needs to be polled
    public static final OID paper_size= new OID("1.3.6.1.2.1.43.8.2.12.0");  // static, but needs to be polled
    public static final OID black = new OID("1.3.6.1.2.1.43.8.2.13.0");  // static, but needs to be polled
    public static final OID red = new OID("1.3.6.1.2.1.43.8.2.14.0");  // static, but needs to be polled
    public static final OID green = new OID("1.3.6.1.2.1.43.8.2.15.0");  // static, but needs to be polled
    public static final OID blue = new OID("1.3.6.1.2.1.43.8.2.16.0");  // static, but needs to be polled
    public static final OID[] colourlist = new OID[]{vendor, model, serial, name, max_capacity, capacity_level, localisation, supports_colour, colour_printing, status, jobs, paper_size, black, red, green, blue};
    public static final OID[] bwlist = new OID[]{vendor, model, serial, name, max_capacity, capacity_level, localisation, supports_colour, colour_printing, status, jobs, paper_size, black};
    // default construction values for the intitial creation of the simulated object
    private final String printer_name;
    private final String[] printer_info; // length 3
    private final int[] colour_values; // length 1 or length 4
    private final int[] capacity; // length 2
    private final boolean colour;
    private final String stat;
    private final String paper_type;
    private final int jobs_complete;

    public PrinterAgent(int identifier, String address, String printer_name, String[] printer_info, int[] capacity, boolean colour, int[] colour_values, String status, int jobs, String paper_size) throws IOException {
        super(identifier, address);
        this.printer_name = printer_name;
        this.printer_info = printer_info;
        this.capacity = capacity;
        this.colour = colour;
        this.stat = status;
        this.jobs_complete = jobs;
        this.colour_values = colour_values;
        this.paper_type = paper_size;
    }

    @Override
    protected void registerManagedObjects() {
        try {
            server.register(MoCreation.createReadOnly(vendor, printer_info[0]), null);
            server.register(MoCreation.createReadOnly(model, printer_info[1]), null);
            server.register(MoCreation.createReadOnly(serial, printer_info[2]), null);
            server.register(MoCreation.createReadWrite(name, printer_name), null);
            server.register(MoCreation.createReadOnly(max_capacity, capacity[0]), null);
            server.register(MoCreation.createReadOnly(capacity_level, capacity[1]), null);
            server.register(MoCreation.createReadWrite(localisation, printer_info[3]), null);
            server.register(MoCreation.createReadOnly(supports_colour, colour), null);
            server.register(MoCreation.createReadWrite(colour_printing, ((colour) ? (colour_values[0] > 0 && colour_values[1] > 0 && colour_values[2] > 0) : false)), null);
            server.register(MoCreation.createReadOnly(status, stat), null);
            server.register(MoCreation.createReadOnly(jobs, jobs_complete), null);
            server.register(MoCreation.createReadWrite(paper_size, paper_type), null);
            server.register(MoCreation.createReadOnly(black, colour_values[0]), null);

            if (colour) {
                server.register(MoCreation.createReadOnly(red, colour_values[0]), null);
                server.register(MoCreation.createReadOnly(green, colour_values[1]), null);
                server.register(MoCreation.createReadOnly(blue, colour_values[2]), null);
            }
        } catch (DuplicateRegistrationException ex) {
            throw new RuntimeException(ex);
        }

    }

    @Override
    protected void unregisterManagedObjects() {
    }
}
